package com.firefighting.maintenancer;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.firefighting.util.JdbcUtils;
import com.firefighting.util.MYJSONObject;

/**
 * Servlet implementation class DeleteTask
 */
@WebServlet(asyncSupported = true, urlPatterns = { "/DeleteTask" })
public class DeleteTask extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		System.out.println("-----"+format.format(new Date())+"    "+this.getServletName()+"-----");
		
		String userId=request.getParameter("userId");
		String task_id = request.getParameter("task_id");
		
		PrintWriter out = response.getWriter();
		MYJSONObject result_jb = new MYJSONObject();
		
		
		Connection connection = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql=null;
		try {
			connection = JdbcUtils.getConnection();
			sql ="update task set task.status=? where task.id=? and task.app_id_account=? ";
			ps = connection.prepareStatement(sql);
			ps.setString(1, "待接收");
			ps.setString(2, task_id);
			ps.setString(3, userId);
			if(ps.executeUpdate()>0)
				{
				System.out.println("task表中的任务状态已改为未完成");
				
				}
				else {
					System.out.println("task表中没有该任务");
				}
			/*sql ="select * from location where task_item.task_id=? ";
			ps = connection.prepareStatement(sql);
			ps.setString(1, task_id);
			rs=ps.executeQuery();
			while(rs.next())
			{
			sql ="delete from location where location.task_id=? ";
			ps = connection.prepareStatement(sql);
			ps.setString(1, task_id);
			if(ps.executeUpdate()>0)
				{
				System.out.println("location表中的数据已经删除");
				}
				else {
					System.out.println("location表中没有数据与任务表关联");
				}
			}
			ps.close();
			rs.close();
		    sql ="delete from task where task.id=? and task.app_id_account=?";
			ps = connection.prepareStatement(sql);
			ps.setString(1, task_id);
			ps.setString(2, userId);
			if (ps.executeUpdate() > 0) {
				ps.close();
				sql ="select * from task_item where task_item.task_id=? ";
				ps = connection.prepareStatement(sql);
				ps.setString(1, task_id);
				rs=ps.executeQuery();
				while(rs.next())
				{
				ps.close();
				sql ="delete from task_item where task_item.task_id=? ";
				ps = connection.prepareStatement(sql);
				ps.setString(1, task_id);
				ps.executeUpdate();
				}*/
				ps.close();
				/*sql ="delete from location where location.task_id=? ";
				ps = connection.prepareStatement(sql);
				ps.setString(1, task_id);
				ps.executeUpdate();*/
				result_jb.put("status", "true");
				result_jb.put("message", "task表中的任务状态已改为待执行");
				System.out.println("该任务内容状态改为待执行");
			/*}else {
				result_jb.put("status", "false");
				result_jb.put("message", "数据库中没有该条记录");
				System.out.println("删除数据出错");
			}*/
			
		} catch (SQLException e) {
			e.printStackTrace();
			result_jb.put("status", "false");
			result_jb.put("message", "服务器异常");
		} finally {
			JdbcUtils.free(rs, ps, connection);
			out.print(result_jb.toString());
			out.flush();
			out.close();
		}
	}

}
